<template>
  <div class="app-container">
    <el-card>
      <div style="display: flex; align-items: center; margin-bottom: 10px">
        <div style="font-weight: bold">到期提醒设置</div>
        <div style="margin-left: auto">
          <el-button
            style="height: 40px; width: 40px; padding: 0"
            icon="el-icon-refresh"
            @click="getList()"
          />
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="ExpirationReminderSetupList"
        @selection-change="handleSelectionChange"
        style="width: 100%; border: 1px solid #ccc"
        :header-cell-style="{
          background: 'rgb(224, 241, 255)',
          color: '#606266',
        }"
      >
        <el-table-column label="提醒类型" align="center" prop="type" />
        <el-table-column label="是否提醒" align="center" prop="state">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              @change="changeTime(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="提醒时间" align="center">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.alarmTime"
              multiple
              placeholder="请选择"
              style="line-height: 50px; border: none; height: 50px; width: 100%"
              @change="changeTime(scope.row)"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改合同到期提醒设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="提醒类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入提醒类型" />
        </el-form-item>
        <el-form-item label="是否提醒" prop="state">
          <el-input v-model="form.state" placeholder="请输入是否提醒" />
        </el-form-item>
        <el-form-item label="是否提前7天提醒" prop="sevenReminder">
          <el-input
            v-model="form.sevenReminder"
            placeholder="请输入是否提前7天提醒"
          />
        </el-form-item>
        <el-form-item label="是否提前30天提醒" prop="thirtyReminder">
          <el-input
            v-model="form.thirtyReminder"
            placeholder="请输入是否提前30天提醒"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExpirationReminderSetup,
  getExpirationReminderSetup,
  delExpirationReminderSetup,
  addExpirationReminderSetup,
  updateExpirationReminderSetup,
} from "@/api/contract/ExpirationReminderSetup";

export default {
  name: "ExpirationReminderSetup",
  data() {
    return {
      optionsValue: [],
      options: [
        {
          label: "提前7天",
          value: "提前7天",
        },
        {
          label: "提前30天",
          value: "提前30天",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同到期提醒设置表格数据
      ExpirationReminderSetupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        state: null,
        sevenReminder: null,
        thirtyReminder: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeTime(row) {
      console.log("33333", this.ExpirationReminderSetupList);
      console.log("22222", row);
      this.form = JSON.parse(JSON.stringify(row));
      if (row.state == false) {
        console.log("1");
        this.form.state = 0;
      } else if (row.state == true) {
        console.log("2");
        this.form.state = 1;
      }
      this.form.alarmTime = row.alarmTime.join(",");
      if (row.alarmTime == "") {
        this.form.alarmTime = "0,0";
      }
      this.form.alarmTime = this.form.alarmTime.replace(/[^0-9,]/g, "");
      console.log("11111", this.form);
      updateExpirationReminderSetup(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 查询合同到期提醒设置列表 */
    getList() {
      this.loading = true;
      listExpirationReminderSetup(this.queryParams).then((response) => {
        this.ExpirationReminderSetupList = response.rows.map((item) => {
          item.alarmTime = item.alarmTime
            .split(",")
            .filter((time) => time !== "0")
            .map((time) => "提前" + time + "天");
          if (item.state === "1") {
            item.state = true;
          } else if (item.state === "0") {
            item.state = false;
          }
          return item;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        expirationReminderSetupId: null,
        type: null,
        state: null,
        sevenReminder: null,
        thirtyReminder: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.expirationReminderSetupId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同到期提醒设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const expirationReminderSetupId =
        row.expirationReminderSetupId || this.ids;
      getExpirationReminderSetup(expirationReminderSetupId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同到期提醒设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.expirationReminderSetupId != null) {
            updateExpirationReminderSetup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpirationReminderSetup(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const expirationReminderSetupIds =
        row.expirationReminderSetupId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除合同到期提醒设置编号为"' +
            expirationReminderSetupIds +
            '"的数据项？'
        )
        .then(function () {
          return delExpirationReminderSetup(expirationReminderSetupIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/ExpirationReminderSetup/export",
        {
          ...this.queryParams,
        },
        `ExpirationReminderSetup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-input--medium .el-input__inner {
  line-height: 40px;
  border: none;
}
/*表格鼠标悬停的样式（背景颜色）*/
::v-deep .el-table tbody tr:hover > td {
  background-color: rgb(255, 255, 255) !important;
}
::v-deep .el-select__tags-text {
  font-size: 14px;
}
</style>
